package com.ca.project.Server.Modules.UserInfo.Controller;

import com.ca.project.Server.Modules.UserInfo.Context.FindUserInfoContext;
import com.ca.project.Server.Modules.UserInfo.Context.UserInfoUpdateContext;
import com.ca.project.Server.Modules.UserInfo.Converter.UserInfoConverter;
import com.ca.project.Server.Modules.UserInfo.Entity.UserInfo;
import com.ca.project.Server.Modules.UserInfo.PO.FindUserInfoPO;
import com.ca.project.Server.Modules.UserInfo.PO.UserInfoUpdatePO;
import com.ca.project.Server.Modules.UserInfo.Service.UserInfoService;
import com.ca.project.Server.Modules.UserInfo.VO.FindUserInfoVo;
import com.ca.project.core.Respond.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description
 * @Author:你好呀小陳同學
 * @CreateTime:22/9/202314:02
 */
@RestController
@RequestMapping("/userinfo")
@Api(tags = "小程序用户信息模块")
public class UserInfoController {

    @Autowired
    private UserInfoService userInfoService;
    @Resource
    private UserInfoConverter userInfoConverter;

    @ApiOperation(
            value = "微信小程序用户更新个人信息接口",
            notes = "该接口提供了用户更新个人信息的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/info")
    public R UserInfoUpdate(@Validated @RequestBody UserInfoUpdatePO updatePO){
        UserInfoUpdateContext userInfoUpdateContext = userInfoConverter.userInfoUpdatePO2Update(updatePO);
        userInfoService.updateInfo(userInfoUpdateContext);
        return R.success();
    }

    @ApiOperation(
            value = "微信小程序用户检索个人信息接口",
            notes = "该接口提供了用户检索个人信息的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/find")
    public R FindUserInfo(@Validated @RequestBody FindUserInfoPO findUserInfoPO){
        String token = findUserInfoPO.getToken();
        UserInfo entity = userInfoService.find(token);
        return R.data(entity);
    }

    @ApiOperation(
            value = "微信小程序通过openid找出联系电话接口",
            notes = "该接口提供了微信小程序通过openid找出联系电话的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PostMapping("/openid")
    public R FindUserInfoByOpenid(@Validated @RequestBody FindUserInfoVo findUserInfoVo){
        String openid = findUserInfoVo.getR_openid();
        String phone = userInfoService.getPhone(openid);
        return R.data(phone);
    }

}
